<template>
  <div class="login_mian">
    <div class="login_welcome">
      <img src="~@/assets/logo.png">
      <p class="login_max_title">欢迎！</p>
      <p class="login_min_title">登录人资管理</p>
    </div>
    <div class="lgoin_form">
      <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit" >
      <h1 style="font-size: 26px;font-weight: 800;color: #000000;text-align:center;margin-bottom:50px">登录</h1>
      <a-alert v-if="isLoginError" type="error" closable showIcon style="margin-bottom: 24px;" message="账户或密码错误" />
      <a-form-item>
        <a-input
          size="large"
          type="text"
          class="other-input"
          placeholder="账户: admin"
          v-decorator="[
            'loginName',
            {rules: [{ required: true, message: '请输入帐户名' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
          ]"
        >
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input>
      </a-form-item>
      <a-form-item>
        <a-input-password
          size="large"
          type="password"
          class="other-input"
          autocomplete="false"
          placeholder="密码: admin or ant.design"
          v-decorator="[
            'password',
            {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
          ]"
        >
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input-password>
      </a-form-item>
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >确定</a-button>
      </a-form-item>
    </a-form>


    </div>
  </div>
</template>

<script>
import md5 from 'md5'
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import Vue from 'vue'


export default {
  data () {
    return {
      loginBtn: false,
      isLoginError: false,
      form: this.$form.createForm(this),
      state: {
        loginBtn: false,
        loginType: 0,
        smsSendBtn: false
      },
      obj:{}
    }
  },
  created () {
  },
  methods: {
    ...mapActions(['Login']),
    handleUsernameOrEmail (rule, value, callback) {
      const { state } = this
      const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      if (regex.test(value)) {
        state.loginType = 0
      } else {
        state.loginType = 1
      }
      callback()
    },
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        Login
      } = this

      state.loginBtn = true

      const validateFieldsKey = ['loginName', 'password']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = { ...values }
          delete loginParams.loginName
          loginParams.loginName = values.loginName
          loginParams.password = md5(values.password)
          Login(loginParams)
          .then((res) => this.loginSuccess(res))
          .catch(err => this.requestFailed(err))
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    stepCaptchaSuccess () {
      this.loginSuccess()
    },
    loginSuccess (res) {
      this.$router.push({ path: '/humanResources/report/compositeEcharts' })
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
    requestFailed (err) {
      this.isLoginError = true
      this.$notification['error']({
        message: '错误',
        description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
<!---scoped-->
<style lang="less">
  .login_mian{
    transform:translate(-50%,-50%);
    position: absolute;
    left: 50%;
    top: 50%;
    width: 812px;
    height: 558px;
    display: flex;
    .login_welcome{
      width: 308px;
      height: 558px;
      background: #1F90FF;
      opacity: 0.8;
      position: relative;
      img{
        position: absolute;
        left: 50%;
        top: 165px;
        transform:translate(-50%,0);
        width: 200px;
      }
      .login_max_title{
         position: absolute;
        left: 50%;
        top: 278px;
        transform:translate(-50%,0);
        font-size: 26px;
        font-family: PingFang SC;
        font-weight: 800;
        color: #FFFFFF;
      }
      .login_min_title{
        position: absolute;
        left: 50%;
        top: 327px;
        transform:translate(-50%,0);
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #FFFFFF;
      }
    }
    .lgoin_form{
        flex: 1;
        background: #fff;
        position: relative;
        .user-layout-login {
          position: absolute;
          left: 50%;
          top: 50%;
          transform:translate(-50%,-50%);
          width: 350px;
          label {
            font-size: 14px;
          }
          .forge-password {
            font-size: 14px;
          }
          .other-input{
            border-bottom:1px solid #ddd;
            .ant-input{
              border:0;
            }
          }
          button.login-button {
            padding: 0 15px;
            font-size: 16px;
            height: 40px;
            width: 100%;
            border-radius: 25px;
            margin-top: 50px;
          }
          .user-login-other {
            text-align: left;
            margin-top: 24px;
            line-height: 22px;
            .item-icon {
              font-size: 24px;
              color: rgba(0, 0, 0, 0.2);
              margin-left: 16px;
              vertical-align: middle;
              cursor: pointer;
              transition: color 0.3s;

              &:hover {
                color: #1890ff;
              }
            }
            .register {
              float: right;
            }
        }
      }
    }
    
  }

</style>
